package com.ziki.bswicket.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.head.CssReferenceHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem;
import org.apache.wicket.markup.head.filter.FilteredHeaderItem;
import org.apache.wicket.markup.head.filter.JavaScriptFilteredIntoFooterHeaderResponse;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.resource.JQueryResourceReference;

import com.ziki.bswicket.model.JsCssInfo;
import com.ziki.framework.wicket.markup.html.FwWebPage;

public class BsPage extends FwWebPage {
    private static final long serialVersionUID = 1L;
    protected String contextPath = this.getRequest().getContextPath();
    private List<JsCssInfo> jsList = new ArrayList<JsCssInfo>();
    private List<JsCssInfo> cssList = new ArrayList<JsCssInfo>();

    public BsPage() {
    }

    public BsPage(IModel<?> model) {
        super(model);
    }

    public BsPage(PageParameters parameters) {
        super(parameters);
    }

    public void addJavaScript(String url) {
        addJavaScript(url, null);
    }

    public void addJavaScript(String url, String condition) {
        addJavaScript(url, null, condition, false);
    }

    public void addJavaScript(String url, boolean isTop) {
        addJavaScript(url, null, isTop);
    }

    public void addJavaScript(String url, String condition, boolean isTop) {
        addJavaScript(url, null, condition, isTop);
    }

    public void addJavaScript(String url, String id, String condition, boolean isTop) {
        JsCssInfo js = new JsCssInfo();
        js.setId(id);
        js.setUrl(url);
        js.setCondition(condition);
        js.setTop(isTop);
        jsList.add(js);
    }

    public void addCss(String url) {
        addCss(url, null);
    }

    public void addCss(String url, String condition) {
        addCss(url, null, condition);
    }

    public void addCss(String url, String media, String condition) {
        JsCssInfo css = new JsCssInfo();
        css.setUrl(url);
        css.setMedia(media);
        css.setCondition(condition);
        cssList.add(css);
    }

    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);

        response.render(new FilteredHeaderItem(JavaScriptHeaderItem.forReference(JQueryResourceReference.get()),
                JavaScriptFilteredIntoFooterHeaderResponse.HEADER_FILTER_NAME));

        for (JsCssInfo css : cssList) {
            if (StringUtils.isNotBlank(css.getUrl())) {
                response.render(CssReferenceHeaderItem.forUrl(css.getUrl(), css.getMedia(), css.getCondition()));
            }
        }
        for (JsCssInfo js : jsList) {
            if (StringUtils.isNotBlank(js.getUrl())) {
                if (js.isTop()) {
                    response.render(new FilteredHeaderItem(JavaScriptReferenceHeaderItem.forUrl(js.getUrl(), js.getId(),
                            false, null, js.getCondition()), JavaScriptFilteredIntoFooterHeaderResponse.HEADER_FILTER_NAME));
                } else {
                    response.render(JavaScriptReferenceHeaderItem.forUrl(js.getUrl(), js.getId(), false, null,
                            js.getCondition()));
                }
            }
        }
    }
}
